home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / TextBoundDemo.java < prev    next >
Text File  |  1998-09-15  |  10KB  |  598 lines

  1. /*
  2.  * @(#)TextBoundDemo.java     1.1 98/06/16
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. import java.applet.Applet;
  32. import java.awt.event.*;
  33. import java.awt.*;
  34. import java.util.*;
  35. import java.text.*;
  36.  
  37.  
  38. public class TextBoundDemo extends DemoApplet
  39. {
  40.     public static void main(String argv[]) {
  41.         DemoApplet.showDemo(new TextBoundFrame(null));
  42.     }
  43.  
  44.     public Frame createDemoFrame(DemoApplet applet) {
  45.         return new TextBoundFrame(applet);
  46.     }
  47. }
  48.  
  49.  
  50.  
  51. class TextBoundFrame extends Frame implements WindowListener, ItemListener , KeyListener
  52. {
  53.  
  54.     private static final String creditString =
  55.  
  56.         "v1.1a9, Demo";
  57.  
  58.     private static final String copyrightString =
  59.  
  60.         "";
  61.  
  62.     private static final String copyrightString2 =
  63.  
  64.         "";
  65.  
  66.  
  67.  
  68.     private static final int FIELD_COLUMNS = 45;
  69.  
  70.     private static final Font choiceFont = null;
  71.  
  72.     private static final boolean DEBUG = false;
  73.  
  74.     private DemoApplet applet;
  75.  
  76.  
  77.     final String right = "-->";
  78.  
  79.     final String left = "<--";
  80.  
  81.  
  82.  
  83.     private BreakIterator enum;
  84.  
  85.  
  86.  
  87.     TextArea text;
  88.  
  89.     Choice bound;
  90.  
  91.  
  92.  
  93.     public TextBoundFrame(DemoApplet applet)
  94.  
  95.     {
  96.         this.applet = applet;
  97.     addWindowListener(this);
  98.         init();
  99.         start();
  100.  
  101.     }
  102.  
  103.  
  104.  
  105.     public void run()
  106.  
  107.     {
  108.  
  109.     }
  110.  
  111.  
  112.  
  113.     public int getSelectionStart()
  114.  
  115.     {
  116.  
  117.         return text.getSelectionStart() & 0x7FFF;
  118.  
  119.     }
  120.  
  121.  
  122.  
  123.     public int getSelectionEnd()
  124.  
  125.     {
  126.  
  127.         return text.getSelectionEnd() & 0x7FFF;
  128.  
  129.     }
  130.  
  131.  
  132.  
  133.     public synchronized void checkChange()
  134.  
  135.     {
  136.  
  137.         int e = enum.following(getSelectionStart());
  138.  
  139.         int s = enum.previous();
  140.  
  141.         selectRange(s, e);
  142.  
  143.     }
  144.  
  145.  
  146.  
  147.     public final synchronized void selectRange(int s, int e)
  148.  
  149.     {
  150.  
  151.         try {
  152.  
  153.             //if (getSelectionStart() != s || getSelectionEnd() != e) {
  154.  
  155.                 text.select(s, e);
  156.  
  157.             //}
  158.  
  159.             if (getSelectionStart() != s || getSelectionEnd() != e) {
  160.  
  161.                 System.out.println("AGH! select("+s+","+e+") -> ("+
  162.  
  163.                 getSelectionStart()+","+getSelectionEnd()+")");
  164.  
  165.             }
  166.  
  167.         } catch (Exception exp) {
  168.  
  169.             errorText(exp.toString());
  170.  
  171.         }
  172.  
  173.     }
  174.  
  175.  
  176.  
  177.     public void init()
  178.  
  179.     {
  180.  
  181.         buildGUI();
  182.  
  183.     }
  184.  
  185.  
  186.  
  187.     public void start()
  188.  
  189.     {
  190.  
  191.     }
  192.  
  193.  
  194.     void addWithFont(Container container, Component foo, Font font) {
  195.  
  196.         if (font != null)
  197.  
  198.             foo.setFont(font);
  199.  
  200.         container.add(foo);
  201.  
  202.     }
  203.  
  204.  
  205.  
  206.    public void buildGUI()
  207.   {
  208.       setBackground(Color.white);
  209.  
  210.       setLayout(new BorderLayout());
  211.  
  212.       Panel topPanel = new Panel();
  213.  
  214.       Label titleLabel = new Label("Text Boundary Demo", Label.CENTER);
  215.  
  216.       titleLabel.setFont(Utility.titleFont);
  217.  
  218.       topPanel.add(titleLabel);
  219.  
  220.       Label demo=new Label(creditString, Label.CENTER);
  221.  
  222.       demo.setFont(Utility.creditFont);
  223.  
  224.       topPanel.add(demo);
  225.  
  226.       Panel choicePanel = new Panel();
  227.  
  228.       Label demo1=new Label("Boundaries", Label.LEFT);
  229.  
  230.       demo1.setFont(Utility.labelFont);
  231.  
  232.       choicePanel.add(demo1);
  233.  
  234.       bound = new Choice();
  235.  
  236.       bound.addItemListener(this);
  237.  
  238.       bound.addItem("Sentence");
  239.  
  240.       bound.addItem("Line Break");
  241.  
  242.       bound.addItem("Word");
  243.  
  244.       bound.addItem("Char");
  245.  
  246.       if (choiceFont != null)
  247.     bound.setFont(choiceFont);
  248.  
  249.       choicePanel.add(bound);
  250.  
  251.       topPanel.add(choicePanel);
  252.  
  253.       Utility.fixGrid(topPanel,1);
  254.  
  255.       add("North", topPanel);
  256.  
  257.       int ROWS = 15;
  258.  
  259.       int COLUMNS = 50;
  260.  
  261.       text = new TextArea(getInitialText(), ROWS, COLUMNS);
  262.  
  263.       text.addKeyListener(this);
  264.  
  265.       text.setEditable(true);
  266.  
  267.       text.selectAll();
  268.  
  269.       text.setFont(Utility.editFont);
  270.  
  271.       add("Center", text);
  272.  
  273.       Panel copyrightPanel = new Panel();
  274.  
  275.       addWithFont (copyrightPanel, 
  276.            new Label(copyrightString, Label.LEFT),Utility.creditFont);
  277.  
  278.       addWithFont (copyrightPanel,
  279.  
  280.            new Label(copyrightString2, Label.LEFT),Utility.creditFont);
  281.  
  282.       Utility.fixGrid(copyrightPanel,1);
  283.  
  284.       add("South", copyrightPanel);
  285.  
  286.       //layout();
  287.  
  288.       handleEnumChanged();
  289.  
  290.       // (new Thread(this)).start();
  291.  
  292.   }
  293.  
  294.  
  295.  
  296.     public String getInitialText()
  297.  
  298.     {
  299.  
  300.         return
  301.  
  302.     /*
  303.  
  304.             "\"This is a sentence.\" This is not.\" \"because. And go. " +
  305.  
  306.             "This is a simple 012.566,5 sample sentence. \n"+
  307.  
  308.             "It does not have to make any sense as you can see. \n"+
  309.  
  310.             "Nel mezzo del cammin di nostra vita, mi ritrovai in "+
  311.  
  312.                 "una selva oscura. \n"+
  313.  
  314.             "Che la dritta via aveo smarrita. \n"+
  315.  
  316.             "He said, that I said, that you said!! \n"+
  317.  
  318.             "Don't rock the boat.\n\n"+
  319.  
  320.             "Because I am the daddy, that is why. \n"+
  321.  
  322.             "Not on my time (el timo.)! \n"+
  323.  
  324.             "Tab\tTab\rTab\tWow."+
  325.  
  326.             "So what!!\n\n"+
  327.  
  328.             "Is this a question???  " +
  329.  
  330.             "I wonder...Hmm.\n" +
  331.  
  332.             "Harris thumbed down several, including \"Away We Go\" "+
  333.  
  334.                 "(which became the huge success Oklahoma!). \n"+
  335.  
  336.             "One species, B. anthracis, is highly virulent.\n"+
  337.  
  338.             "Wolf said about Sounder: \"Beautifully thought-out and "+
  339.  
  340.                 "directed.\"\n"+
  341.  
  342.             "Have you ever said, \"This is where I shall live\"? \n"+
  343.  
  344.             "He 1000,233,456.000 answered, \"You may not!\" \n"+
  345.  
  346.             "Another popular saying is: \"How do you do?\". \n"+
  347.  
  348.             "What is the proper use of the abbreviation pp.? \n"+
  349.  
  350.             "Yes, I am 1,23.322% definatelly 12\" tall!!";
  351.  
  352.     */
  353.  
  354.             "(\"This is a complete sentence.\") This is (\"not.\") also. \n"
  355.  
  356.             +"An abbreviation in the middle, etc. and one at the end, etc. "+
  357.  
  358.                 "This\n"
  359.  
  360.             +"is a simple sample 012.566,5 sentence. It doesn't\n"
  361.  
  362.             +"have to make any sense, as you can see. Nel mezzo del \nc"
  363.  
  364.             +"ammin di nostra vita, mi ritrovai in una selva oscura. Che\n"
  365.  
  366.             +"la dritta via aveo smarrita. Not on my time (el timo.)! And\n"
  367.  
  368.             +"tabulated columns: \tCol1\tCol2\t3,456%.\t\n"
  369.  
  370.             +"Is this a question???  I wonder... Hmm. Harris thumbed\n"
  371.  
  372.             +"down several, including \"Away We Go\" (which became the \n"
  373.  
  374.             +"huge success Oklahoma!). One species, B. anthracis, is \n"
  375.  
  376.             +"highly virulent. Wolf said about Sounder: \"Beautifully \n"
  377.  
  378.             +"thought-out and directed.\" Have you ever said, \"This is "+
  379.  
  380.                 "where I\n"
  381.  
  382.             +"shall live\"? He said 1000,233,456.000 and answered, \"You "+
  383.  
  384.                 "may not!\" \n"
  385.  
  386.             +"Another popular saying is: \"How do you do?\". What is the \n"
  387.  
  388.             +"proper use of the abbreviation pp.? Yes, I am 12\' 3\" tall!!";
  389.  
  390.     }
  391.  
  392.  
  393.  
  394.  
  395.  
  396.     public void handleEnumChanged()
  397.  
  398.     {
  399.  
  400.         String s = bound.getSelectedItem();
  401.  
  402.         if (s.equals("Char")) {
  403.  
  404.             errorText("getCharacterInstance");
  405.  
  406.             enum = BreakIterator.getCharacterInstance();
  407.  
  408.         }
  409.  
  410.         else if (s.equals("Word")) {
  411.  
  412.             errorText("tWordBreak");
  413.  
  414.             enum = BreakIterator.getWordInstance();
  415.  
  416.         }
  417.  
  418.         else if (s.equals("Line Break")) {
  419.  
  420.             errorText("getLineInstance");
  421.  
  422.             enum = BreakIterator.getLineInstance();
  423.  
  424.         }
  425.  
  426.         else /* if (s.equals("Sentence")) */ {
  427.  
  428.             errorText("getSentenceInstance");
  429.  
  430.             enum = BreakIterator.getSentenceInstance();
  431.  
  432.         }
  433.  
  434.         enum.setText(text.getText());
  435.  
  436.         selectRange(0, 0);
  437.  
  438.         //text.select(0,0);
  439.  
  440.     }
  441.  
  442.  
  443.  
  444.     public void handleForward()
  445.  
  446.     {
  447.  
  448.         try {
  449.  
  450.             enum.setText(text.getText());
  451.  
  452.             int oldStart = getSelectionStart();
  453.  
  454.             int oldEnd = getSelectionEnd();
  455.  
  456.             if (oldEnd < 1) {
  457.  
  458.                 selectRange(0, enum.following(0));
  459.  
  460.             }
  461.  
  462.             else {
  463.  
  464.                 int s = enum.following(oldEnd-1);
  465.  
  466.                 int e = enum.next();
  467.  
  468.                 if (e == -1) {
  469.  
  470.                     e = s;
  471.  
  472.                 }
  473.                 selectRange(s, e);
  474.  
  475.             }
  476.  
  477.             //text.select(s, e);
  478.  
  479.             //errorText("<" + oldStart + "," + oldEnd + "> -> <" +
  480.  
  481.                 //s + "," + e + ">");
  482.  
  483.         }
  484.  
  485.         catch (Exception exp) {
  486.  
  487.             errorText(exp.toString());
  488.  
  489.         }
  490.  
  491.     }
  492.  
  493.  
  494.  
  495.     public void handleBackward()
  496.  
  497.     {
  498.  
  499.         try {
  500.  
  501.             enum.setText(text.getText());
  502.  
  503.             int oldStart = getSelectionStart();
  504.  
  505.             int oldEnd = getSelectionEnd();
  506.  
  507.             if (oldStart < 1) {
  508.  
  509.                 selectRange(0, 0);
  510.  
  511.             }
  512.  
  513.             else {
  514.  
  515.                 int e = enum.following(oldStart-1);
  516.  
  517.                 int s = enum.previous();
  518.  
  519.                 selectRange(s, e);
  520.  
  521.             }
  522.  
  523.             //text.select(s, e);
  524.  
  525.             //errorText("<" + oldStart + "," + oldEnd + "> -> <" + s + "," + e + ">");
  526.  
  527.         }
  528.  
  529.         catch (Exception exp) {
  530.  
  531.             errorText(exp.toString());
  532.  
  533.         }
  534.  
  535.     }
  536.  
  537.   /* ItemListener method */
  538.   public void itemStateChanged(ItemEvent e) {
  539.     if(e.getSource() instanceof Choice)
  540.       handleEnumChanged();
  541.   }
  542.   
  543.   /* KeyListener methods */
  544.   public void keyPressed(KeyEvent e) {
  545.     if (e.getKeyCode() == KeyEvent.VK_RIGHT){
  546.       e.consume();// don't deliver the event to the native widget
  547.       handleForward();
  548.     } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
  549.       e.consume();// don't deliver the event to the native widget
  550.       handleBackward();
  551.     }
  552.   }
  553.  
  554.   public void keyReleased(KeyEvent e) {
  555.   }
  556.  
  557.   public void keyTyped(KeyEvent e) {
  558.   }
  559.   
  560.   /* Window Listener methods */
  561.   public void windowClosed(WindowEvent e) {
  562.   }
  563.  
  564.   public void windowDeiconified(WindowEvent e) {
  565.   }
  566.  
  567.   public void windowIconified(WindowEvent e) {
  568.   }
  569.  
  570.   public void windowActivated(WindowEvent e) {
  571.   }
  572.  
  573.   public void windowDeactivated(WindowEvent e) {
  574.   }
  575.  
  576.   public void windowOpened(WindowEvent e) {
  577.   }
  578.  
  579.   public void windowClosing(WindowEvent e) {
  580.     setVisible(false);
  581.     dispose();
  582.  
  583.     if (applet != null) {
  584.       applet.demoClosed();
  585.     } else System.exit(0);
  586.  
  587.   }
  588.   
  589.  
  590.     public void errorText(String s)
  591.     {
  592.         if (DEBUG)
  593.             System.out.println(s);
  594.     }
  595.  
  596. }
  597.  
  598.